package topic

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/topic"
	topicReq "gitcode.com/G-YT/iHealth25/server/model/topic/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type TopicApi struct {
}

var toPicService = service.ServiceGroupApp.TopicServiceGroup.TopicService

// CreateTopic 创建Topic
// @Tags Topic
// @Summary 创建Topic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body topic.Topic true "创建Topic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /toPic/createTopic [post]
func (toPicApi *TopicApi) CreateTopic(c *gin.Context) {
	var toPic topic.Topic
	err := c.ShouldBindJSON(&toPic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Pid":  {utils.NotEmpty()},
		"Name": {utils.NotEmpty()},
	}
	if err := utils.Verify(toPic, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := toPicService.CreateTopic(&toPic); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteTopic 删除Topic
// @Tags Topic
// @Summary 删除Topic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body topic.Topic true "删除Topic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /toPic/deleteTopic [delete]
func (toPicApi *TopicApi) DeleteTopic(c *gin.Context) {
	var toPic topic.Topic
	err := c.ShouldBindJSON(&toPic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := toPicService.DeleteTopic(toPic); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteTopicByIds 批量删除Topic
// @Tags Topic
// @Summary 批量删除Topic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Topic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /toPic/deleteTopicByIds [delete]
func (toPicApi *TopicApi) DeleteTopicByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := toPicService.DeleteTopicByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateTopic 更新Topic
// @Tags Topic
// @Summary 更新Topic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body topic.Topic true "更新Topic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /toPic/updateTopic [put]
func (toPicApi *TopicApi) UpdateTopic(c *gin.Context) {
	var toPic topic.Topic
	err := c.ShouldBindJSON(&toPic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Pid":  {utils.NotEmpty()},
		"Name": {utils.NotEmpty()},
	}
	if err := utils.Verify(toPic, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := toPicService.UpdateTopic(toPic); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindTopic 用id查询Topic
// @Tags Topic
// @Summary 用id查询Topic
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query topic.Topic true "用id查询Topic"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /toPic/findTopic [get]
func (toPicApi *TopicApi) FindTopic(c *gin.Context) {
	var toPic topic.Topic
	err := c.ShouldBindQuery(&toPic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if retoPic, err := toPicService.GetTopic(toPic.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"retoPic": retoPic}, c)
	}
}

// GetTopicList 分页获取Topic列表
// @Tags Topic
// @Summary 分页获取Topic列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query topicReq.TopicSearch true "分页获取Topic列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /toPic/getTopicList [get]
func (toPicApi *TopicApi) GetTopicList(c *gin.Context) {
	var pageInfo topicReq.TopicSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := toPicService.GetTopicInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetAppTopicList 获取话题列表
// @Tags Topic
// @Summary 分页获取Topic列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query topicReq.TopicSearch true "获取话题列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /toPic/getTopicList [get]
func (toPicApi *TopicApi) GetAppTopicList(c *gin.Context) {
	if list, err := toPicService.GetTopicList(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(list, "获取成功", c)
	}
}
